import axios from 'axios';

import store from '@/store/index';
import router from '@/router';
import { Message } from 'element-ui';

export const baseURL = 'http://big-event-vue-api-t.itheima.net';  //导出基地址


const request = axios.create({
    baseURL:baseURL,  //配置基础路径
    timeout:5000  //请求时间超过5s
})

//请求拦截器
request.interceptors.request.use((config)=>{
    if(store.getters['user/getToken']){
        config.headers['Authorization'] = store.getters['user/getToken'];
    }
    return config;
},(error) => {
    return Promise.reject(error);
})

//响应拦截器
request.interceptors.response.use((response)=>{
    return response.data;
},(error)=>{
    // console.dir(error);  //详细打印error
    if(error.response.status == 401){
        //token过期
        Message({type:"info",message:'用户身份过期!'});
        // router.push("/login");
    }
    return Promise.reject(new Error("请求错误!"))
})


export default request;